package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    DtsCategoryMapper categoryMapper;

    @Autowired
    StatMapper statMapper;

    @Override
    public List<CategorySellAmts> findCategorySellData() {

        List<CategorySellAmts> list = statMapper.findCategorySellData();
        return list;
    }

    /**
     * 1.查询一级分类 2.根据一级分类信息查询对应的二级分类
     * @return
     */
    @Override
    public List<CatVo> findCategoryList() {
        //查询所有一级分类
        LambdaQueryWrapper<DtsCategory> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsCategory::getPid, 0);
        qw1.eq(DtsCategory::getDeleted, false);
        List<DtsCategory> catL1 = categoryMapper.selectList(qw1);

        List<CatVo> catVos = catL1.stream().map(cat -> {
            //cat 表示 每一个一级分类 对象, 对应一个catVo对象
            CatVo catVo = new CatVo();
            //设置分类名称
            catVo.setLabel(cat.getName());
            //设置分类id
            catVo.setValue(cat.getId());

            //设置二级分类
            //查询每个一级分类对应的二级分类信息
            LambdaQueryWrapper<DtsCategory> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DtsCategory::getPid, cat.getId());
            qw2.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> catL2 = categoryMapper.selectList(qw2);

            List<CatVo> catvo2s = catL2.stream().map(cat2 -> {
                //cat2表示每一个二级分类id
                CatVo catVo2 = new CatVo();
                catVo2.setValue(cat2.getId());
                catVo2.setLabel(cat2.getName());
                return catVo2;
            }).collect(Collectors.toList());
            //设置二级分类信息
            catVo.setChildren(catvo2s);

            return catVo;
        }).collect(Collectors.toList());


        return catVos;
    }
}
